package cn.bonoon.controllers.plugins;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("s/editor/upload")
public class EditorUploadController {

//	private String getPath(HttpServletRequest req) {
//		return RelativePathInfo.upload(req, DEFAULT_EDITOR_UPLOAD);
//	}

	private final static String DEFAULT_EDITOR_UPLOAD = "editor";

	private void getFiles(String realpath, StringBuilder imgStr) {
		File realFile = new File(realpath);
		if (realFile.isDirectory()) {
			File[] subfiles = realFile.listFiles();
			for (File file : subfiles) {
				if (file.isDirectory()) {
					getFiles(file.getAbsolutePath(), imgStr);
				} else {
					Pattern reg = Pattern.compile("[.]jpg|png|jpeg|gif$");
					Matcher matcher = reg.matcher(file.getName());
					if (matcher.find()) {
						if (imgStr.length() > 0)
							imgStr.append("ue_separate_ue");
						imgStr.append(file.getName());
					}
				}
			}
		}
	}

	@RequestMapping(value = "imageManager.b", method = { GET, POST })
	public void imageManager(HttpServletRequest req, HttpServletResponse resp,
			String usu) throws IOException {
//		StringBuilder imgStr = new StringBuilder();
//		getFiles(getPath(req), imgStr);
//		resp.getWriter().print(imgStr);
//		resp.getWriter().close();
	}

	@RequestMapping(value = "imageUp.b", method = { GET, POST })
	public void imageUp(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

	}
}
